package cn.phecda.boot.tenant.test.table.mybatis;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * <p> TODO </p>
 *
 * @author : wanpinwei
 */
@Service
@RequiredArgsConstructor
public class PersonTenantMybatisService extends ServiceImpl<PersonTenantMybatisMapper, PersonTenantMybatisEntity> {
    private final PersonTenantMybatisMapper personTenantMyBatisMapper;

    @Async
    @Transactional
    @SneakyThrows
    public void asyncSave(String name) {
        TimeUnit.SECONDS.sleep(3);
        personTenantMyBatisMapper.insert(personTenantMyBatisMapper.named(name));
    }

    @Transactional
    public PersonTenantMybatisEntity save(String name) {
        PersonTenantMybatisEntity person = personTenantMyBatisMapper.named(name);
        personTenantMyBatisMapper.insert(person);
        return person;
    }

    @Transactional
    public void update(String id, String name) {
        PersonTenantMybatisEntity person = personTenantMyBatisMapper.selectById(id);
        person.setName(name);
        personTenantMyBatisMapper.updateById(person);
    }

}
